package com.yyk.yiliao.ui.trade;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.UserbankMybankcardInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;

import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的银行卡
 */
public class MyBankcardActivity extends BaseActivity {
    public static MyBankcardActivity intance;

    @BindView(R.id.rv_cardlist)
    RecyclerView rvCardlist;
    @BindView(R.id.bt_addcard)
    Button btAddcard;


    private List<UserbankMybankcardInfo.DataBean> list = new ArrayList<>();
    private BaseRecyclerAdapter<UserbankMybankcardInfo.DataBean> adapter;


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_bankcard;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("我的银行卡");
    }

    @Override
    public void initData() {
        intance = this;
        initAdapter();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initUserbankMybankcardt(true);
    }

    private void initUserbankMybankcardt(boolean flag) {
        Map<String, String> map = new TreeMap<>();

        map.put("id", Hawk.get("uid") + "");
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUserbankMybankcard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<UserbankMybankcardInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(UserbankMybankcardInfo info) {
                        Logger.e("我的银行卡列表" + info.toString());
                        if (flag) {
                            list.clear();
                        }
                        if (info.getCode() == 1) {
                            if (info.getData().size() > 0 && info.getData() != null) {
                                list.addAll(info.getData());
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                });


    }

    private void initAdapter() {
        //银行卡列表
        adapter = new BaseRecyclerAdapter<UserbankMybankcardInfo.DataBean>(mActivity, list, R.layout.adapter_item_myyhcard) {
            @Override
            public void convert(BaseRecyclerHolder holder, UserbankMybankcardInfo.DataBean item, int position, boolean isScrolling) {
                int cardtype = item.getCardtype();
                if (cardtype == 1) {
                    holder.setText(R.id.tv_cardtype_name, item.getBank_name() + " 信用卡");
                } else {
                    holder.setText(R.id.tv_cardtype_name, item.getBank_name() + " 储蓄卡");
                }


                holder.setText(R.id.tv_numberu, item.getNumberu());
                holder.setImageByUrl(R.id.iv_img, ApiService.BASE_URL + item.getImg());
            }
        };
        rvCardlist.setLayoutManager(new LinearLayoutManager(this));
        rvCardlist.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {

                Bundle bundle = new Bundle();
                int id = list.get(position).getBindid();
                int cardtype = list.get(position).getCardtype();
                String img = list.get(position).getImg();
                String name = list.get(position).getBank_name();
                String numberu = list.get(position).getNumberu();
                bundle.putString("id", id + "");
                bundle.putString("cardtype", cardtype + "");
                bundle.putString("img", img + "");
                bundle.putString("name", name + "");
                bundle.putString("numberu", numberu + "");

                startAct(MyBankcardXQActivity.class, bundle);
            }
        });
    }


    @OnClick(R.id.bt_addcard)
    public void onViewClicked() {
        startAct(AddBankcardActivity.class);
    }
}
